package com.zqg8512.netty.ws;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @Author zengqianggang
 * @Description //TODO
 * Author VPN
 * @Date 2019/9/30 11:45
 * @Version 1.0
 **/
public class ChannelInitialter extends ChannelInitializer<SocketChannel> {

    private final String sign = "init:初始化：";

    private void show(String msg) {
        System.out.println(sign + msg);
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        show("initChannel");

        ChannelPipeline pip = ch.pipeline();

        pip.addLast("HttpServerCodec", new HttpServerCodec());

        pip.addLast("HttpObjectAggregator", new HttpObjectAggregator(1024 * 64));


        pip.addLast("WebSocketServerProtocolHandler", new WebSocketServerProtocolHandler("/wsNetty"));

        ch.pipeline().addLast("http-chunked",new ChunkedWriteHandler());

        pip.addLast("MyChannelHandel", new MyChannelHandel());
    }
}
